{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# ベクトル・行列\n",
    "\n",
    "* Pythonでベクトル演算をする\n",
    "    * numpyを使う"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "from numpy import array as npv\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# matplotlibによるベクトルのプロット\n",
    "\n",
    "numpyを使わずにベクトルをプロット"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGp9JREFUeJzt3X1wVHWe7/H3l/AQeVACRh4EBhVEUGd8QAFFTRCUx4Rw\nsqVzq2adrZlinbvW3b11/5kda2ZvTU3NjjtTWzNTzt0py7FGrS1Hbk6AiCCRh4gOwgWRAQRBREae\nlEeBIAIh3/tHjr1JOoEk3cnpbj6vqi5O9+/X/fv+OCEfTp/TvzZ3R0REpFvcBYiISGZQIIiICKBA\nEBGRiAJBREQABYKIiEQUCCIiAqQhEMxsuJmtNrPtZvaBmf1jC33MzH5rZrvNbIuZ3ZXquCIikl7d\n0/AadcD/cvdNZtYPeM/M3nT37Y36zABGR7cJwH9Ef4qISIZI+QjB3Q+5+6Zo+zSwA7i+WbdS4CVv\nsA7ob2ZDUh1bRETSJx1HCAlmNhK4E1jfrOl6YF+j+/ujxw41e/58YD5Afn7+3SNGjEhneRmlvr6e\nbt1y9xSO5pfdcnl+uTw3gF27dh1198KOPDdtgWBmfYEQ+Cd3P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oEEREBFAgiIhJRIIiICKBAEBGRiAJBREQABYKIiET+P/So+PjX9e52\nAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10c9c0f98>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure()\n",
    "\n",
    "### 矢印（ベクトル）の始点\n",
    "X = 0\n",
    "Y = 0\n",
    "### 矢印（ベクトル）の成分\n",
    "U = 2\n",
    "V = 1\n",
    "\n",
    "### 矢印（ベクトル）\n",
    "plt.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1)\n",
    "\n",
    "### グラフ表示\n",
    "plt.xlim([-1,2])\n",
    "plt.ylim([-1,2])\n",
    "plt.grid()\n",
    "plt.draw()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# numpyによるベクトルの生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1 = [1 3]\n",
      "v2 = [2 5]\n",
      "v3 = [3 8]\n"
     ]
    }
   ],
   "source": [
    "### ベクトル演算をサポートしたarrayインスタンスの生成\n",
    "v1 = npv([1, 3])\n",
    "v2 = npv([2, 5])\n",
    "#print (v1)\n",
    "#print (v2)\n",
    "print (\"v1 = {0}\".format(v1))\n",
    "print (\"v2 = {0}\".format(v2))\n",
    "\n",
    "### ベクトルの足し算\n",
    "v3 = v1 + v2\n",
    "print (\"v3 = {0}\".format(v3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v4 = [13 13]\n"
     ]
    }
   ],
   "source": [
    "### ランダムな値を持つ2次元ベクトルの生成 (0〜1)\n",
    "#v4 = np.random.random(size=(2))\n",
    "\n",
    "### ランダムな値を持つ2次元ベクトルの生成 (10〜15の整数)\n",
    "v4 = np.random.randint(10, 15, size=(2))\n",
    "\n",
    "print (\"v4 = {0}\".format(v4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 正規直交基底\n",
    "\n",
    "http://mathwords.net/seikityokkoukitei\n",
    "\n",
    "## 正規直交基底とは\n",
    "\n",
    "ベクトルの集合 $ \\boldsymbol{v_1}, \\boldsymbol{v_2}, \\cdots, \\boldsymbol{v_k} $が以下の条件を満たすとき、正規直交基底と言う\n",
    "\n",
    "1. それぞれの長さが 1（正規化されている）\n",
    "1. 互いに直交している（内積が 0）\n",
    "1. 基底である（線形結合で全てを表せる）\n",
    "\n",
    "## 正規直交基底だと何がうれしいのか\n",
    "\n",
    "あとで書く\n",
    "\n",
    "\n",
    "## 正規直行基底の具体例\n",
    "\n",
    "以下の3本のベクトルは正規直行基底をなしている\n",
    "\n",
    "$\n",
    "  \\boldsymbol{v_1} = \\left(\n",
    "    \\begin{array}{c}\n",
    "      \\frac{1}{\\sqrt{2}}  \\\\\n",
    "      \\frac{1}{\\sqrt{2}}  \\\\\n",
    "      0 \n",
    "    \\end{array}\n",
    "  \\right)\n",
    "$\n",
    ",\n",
    "$\n",
    "\\boldsymbol{v_2} = \\left(\n",
    "    \\begin{array}{c}\n",
    "      \\frac{1}{\\sqrt{2}}  \\\\\n",
    "      - \\frac{1}{\\sqrt{2}}  \\\\\n",
    "      0 \n",
    "    \\end{array}\n",
    "  \\right)\n",
    "$\n",
    ",\n",
    "$\n",
    "\\boldsymbol{v_3} = \\left(\n",
    "    \\begin{array}{c}\n",
    "      0  \\\\\n",
    "      0  \\\\\n",
    "      1 \n",
    "    \\end{array}\n",
    "  \\right)\n",
    "$\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1 = [ 0.70710678  0.70710678  0.        ]\n",
      "v2 = [ 0.70710678 -0.70710678  0.        ]\n",
      "v3 = [0 0 1]\n"
     ]
    }
   ],
   "source": [
    "### ベクトルを生成\n",
    "v1 = npv([1/np.sqrt(2), 1/np.sqrt(2), 0])\n",
    "v2 = npv([1/np.sqrt(2), - 1/np.sqrt(2), 0])\n",
    "v3 = npv([0, 0, 1])\n",
    "print (\"v1 = {0}\".format(v1))\n",
    "print (\"v2 = {0}\".format(v2))\n",
    "print (\"v3 = {0}\".format(v3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### 1. それぞれの長さが1（正規化されている）\n",
    "\n",
    "$  | \\boldsymbol{v_1} | = \\sqrt{ ( \\frac{1}{\\sqrt{2}} )^2 + \\frac{1}{\\sqrt{2}} )^2 } = 1 $\n",
    "\n",
    "同様に\n",
    "\n",
    "$  | \\boldsymbol{v_2} | =  | \\boldsymbol{v_3} | = 1 $  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|v1| = 0.9999999999999999\n",
      "|v2| = 0.9999999999999999\n",
      "|v3| = 1.0\n"
     ]
    }
   ],
   "source": [
    "norm_v1 = np.linalg.norm(v1)\n",
    "norm_v2 = np.linalg.norm(v2)\n",
    "norm_v3 = np.linalg.norm(v3)\n",
    "\n",
    "print (\"|v1| = {0}\".format(norm_v1))\n",
    "print (\"|v2| = {0}\".format(norm_v2))\n",
    "print (\"|v3| = {0}\".format(norm_v3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### 2. 互いに直交している（内積が 0）\n",
    "\n",
    "\n",
    "$ \\boldsymbol{v_1} \\cdot \\boldsymbol{v_2} = \\frac{1}{\\sqrt{2}} \\cdot \\frac{1}{\\sqrt{2}} + \\frac{1}{\\sqrt{2}} \\cdot - ( \\frac{1}{\\sqrt{2}} ) = 0 $  \n",
    "\n",
    "同様に\n",
    "\n",
    "$ \\boldsymbol{v_1} \\cdot \\boldsymbol{v_3} = \\boldsymbol{v_2} \\cdot \\boldsymbol{v_3} = 0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "v1・v2 = 0.0\n",
      "v1・v3 = 0.0\n",
      "v2・v3 = 0.0\n"
     ]
    }
   ],
   "source": [
    "v1_dot_v2 = np.dot(v1, v2)\n",
    "v1_dot_v3 = np.dot(v1, v3)\n",
    "v2_dot_v3 = np.dot(v2, v3)\n",
    "\n",
    "print (\"v1・v2 = {0}\".format(v1_dot_v2))\n",
    "print (\"v1・v3 = {0}\".format(v1_dot_v3))\n",
    "print (\"v2・v3 = {0}\".format(v2_dot_v3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "### 3. 基底である（線形結合で全てを表せる）\n",
    "\n",
    "* 基底とは\n",
    "    * 線型独立なベクトルから成る集合で、そのベクトルの（有限個の）線型結合として、与えられたベクトル空間の全てのベクトルを表すことができるもの\n",
    "\n",
    "三本のベクトルを並べた行列が正則であることを確認できればよい\n",
    "\n",
    "$\n",
    "  ( \\boldsymbol{v_1} \\boldsymbol{v_2} \\boldsymbol{v_3} ) = \\left(\n",
    "    \\begin{array}{ccc}\n",
    "      \\frac{1}{\\sqrt{2}} & \\frac{1}{\\sqrt{2}} & 0 \\\\\n",
    "      \\frac{1}{\\sqrt{2}} & - \\frac{1}{\\sqrt{2}} & 0 \\\\\n",
    "      0 & 0 & 1\n",
    "    \\end{array}\n",
    "  \\right)\n",
    "$\n",
    "\n",
    "* 正則である\n",
    "    * http://mathtrain.jp/seisokumatrix\n",
    "        * 条件のいずれかを満たせば良い\n",
    "    * http://physmath.main.jp/src/inverse-matrix.html\n",
    "        * 逆行列をもつ行列を一般的に正則行列と呼ぶ\n",
    "    * いずれ全ての条件やrankについても別個にまとめても良いかも"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "matrix_v1v2v3 = \n",
      " [[ 0.70710678  0.70710678  0.        ]\n",
      " [ 0.70710678 -0.70710678  0.        ]\n",
      " [ 0.          0.          1.        ]] \n",
      "\n",
      "det_matrix_v1v2v3 = \n",
      " -0.9999999999999999 \n",
      "\n",
      "inv_matrix_v1v2v3 = \n",
      " [[ 0.70710678  0.70710678  0.        ]\n",
      " [ 0.70710678 -0.70710678 -0.        ]\n",
      " [ 0.          0.          1.        ]] \n",
      "\n",
      "matrix_v1v2v3_product_inv_matrix_v1v2v3 = \n",
      " [[  1.00000000e+00   1.01465364e-17   0.00000000e+00]\n",
      " [  1.01465364e-17   1.00000000e+00   0.00000000e+00]\n",
      " [  0.00000000e+00   0.00000000e+00   1.00000000e+00]] \n",
      "\n",
      "inv_matrix_v1v2v3_matrix_v1v2v3_product = \n",
      " [[  1.00000000e+00   1.01465364e-17   0.00000000e+00]\n",
      " [  1.01465364e-17   1.00000000e+00   0.00000000e+00]\n",
      " [  0.00000000e+00   0.00000000e+00   1.00000000e+00]] \n",
      "\n"
     ]
    }
   ],
   "source": [
    "### 3×3行列を生成\n",
    "matrix_v1v2v3 = np.c_[v1, v2, v3]\n",
    "print (\"matrix_v1v2v3 = \\n {0} \\n\".format(matrix_v1v2v3))\n",
    "\n",
    "### 行列式\n",
    "det_matrix_v1v2v3 = np.linalg.det(matrix_v1v2v3)\n",
    "print (\"det_matrix_v1v2v3 = \\n {0} \\n\".format(det_matrix_v1v2v3))\n",
    "\n",
    "### 逆行列\n",
    "inv_matrix_v1v2v3 = np.linalg.inv(matrix_v1v2v3)\n",
    "print (\"inv_matrix_v1v2v3 = \\n {0} \\n\".format(inv_matrix_v1v2v3))\n",
    "\n",
    "### 行列の積\n",
    "matrix_v1v2v3_product_inv_matrix_v1v2v3 = np.dot(matrix_v1v2v3, inv_matrix_v1v2v3)\n",
    "print (\"matrix_v1v2v3_product_inv_matrix_v1v2v3 = \\n {0} \\n\".format(matrix_v1v2v3_product_inv_matrix_v1v2v3))\n",
    "\n",
    "### 順番を変えた行列の積\n",
    "inv_matrix_v1v2v3_product_matrix_v1v2v3 = np.dot(inv_matrix_v1v2v3, matrix_v1v2v3)\n",
    "print (\"inv_matrix_v1v2v3_matrix_v1v2v3_product = \\n {0} \\n\".format(inv_matrix_v1v2v3_product_matrix_v1v2v3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 内積\n",
    "\n",
    "http://lab.adn-mobasia.net/?p=4057\n",
    "\n",
    "$ \\boldsymbol{A} \\cdot \\boldsymbol{B} = \\Sigma A_i B_i$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.52987941  0.23646249]\n",
      "0.33668669964\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "### ランダムな値を持つ2次元ベクトルの生成\n",
    "A_vec = np.random.random(size=(2))\n",
    "print (A_vec)\n",
    "\n",
    "### 自分自身の内積\n",
    "print (np.dot(A_vec, A_vec))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 正規直交基底\n",
    "\n",
    "#http://blog.physips.com/entry/fourier_orthogonal\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
